package m202405.d13;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/5/13 12:43
 */
public class Q543 {
    public int diameterOfBinaryTree(TreeNode root) {
        max = 1;
        maxDepth(root);
        return max - 1;
    }

    int max = 0;

    private int maxDepth(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int l = maxDepth(node.left);
        int r = maxDepth(node.right);
        max = Math.max(max, l + r + 1);
        return Math.max(l, r) + 1;
    }
}
